#include "ebm_env.h"
#include "bconn.h"

#define EBM_NET_KEEP_WCNT_VAL 10
static unsigned char ebm_net_keep_wcnt = 0;

void command_hearttick_wcnt_clear(void)
{
	ebm_net_keep_wcnt= EBM_NET_KEEP_WCNT_VAL;
}

static unsigned char command_hearttick_wcnt_tick(void)
{
	unsigned char ret=0;
	if(ebm_net_keep_wcnt == 1)
		ret= 1;
	if(ebm_net_keep_wcnt > 0)
		ebm_net_keep_wcnt--;
	return ret;
}

static unsigned char command_heart_ticks_get(unsigned short *ticks)
{
	unsigned ret=0;
	unsigned short n_sec;
	n_sec= ebm_env_get_heart_tp();
	if(n_sec != 0)
		ret =1;
	
	if(n_sec < 10 ){
		*ticks= 10;
	}
	else{
		*ticks= n_sec;
	}
	return ret;
}

unsigned short command_pack_hearttick(unsigned char *data)
{
static unsigned int last_session_id= 1;
	unsigned char *pdata;
	MSG_FIXED_HEADER *msg;
	HEART_TICK_CONTENT* business;
	msg= (MSG_FIXED_HEADER*)data;

	msg->head_flag= 0xFDFE;
	msg->version= 0x0001;
	msg->session_id= swap32(last_session_id);
	last_session_id++;
	msg->type= MSG_TYPE_REQ;
	msg->sign_flag= 0;
	msg->data_len= swap16(0x82);
	
	ebm_env_get_sourceid(msg->src_socde, 12);
	
	msg->items=swap16(1);
	memset(msg->items_arry, 0, 12);
	
	msg->business_type= PASSBACK_HEARTTICK;/*需要更新*/
	msg->business_len= swap16(0x09);
	business= (HEART_TICK_CONTENT*)&msg->business_data;

	business->sta= ebm_env_get_playsta();
	business->register_1st_time= ebm_env_get_register_1st_time();
	business->phyid_len=6;
	
	ebm_env_get_phyid(business->phyid, business->phyid_len);
	
	pdata= business->phyid;
	pdata+=6;
	pdata[0]= 0x00;
	pdata[1]= 0x4a;

	memset(pdata+2, 0, 0x4a);

	unsigned int *p_crc= (unsigned int*) (data+ 126);

	unsigned int crc_val= nstar_crc32_ts(data, 126);
	*p_crc= swap32(crc_val);

	//command_net_send(data, 130);
	if(1)
	{
		int i;
		ebmdbg_command("send heart msg:\n");
		for(i=0; i< 130;i++){
			ebmdbg_command("%02X ", data[i]);
		}
		ebmdbg_command("\n");
	}
    return 130;
}


static int command_rec(unsigned char *data, unsigned int len) {
    handle_ebm_msg(data, len);
    return 0;
}


static void command_send_func(struct bsockt *b) {
    unsigned char data[200]; unsigned short len;
    if(PEND_T_S()){
        unsigned char is_vaild; unsigned short ticks;
        len = command_pack_hearttick(data);
        bconn_send(b, data, len);
        is_vaild= command_heart_ticks_get(&ticks);
        if(is_vaild && command_hearttick_wcnt_tick()){
            printf("command net wcnt time out\n");
            sys_log("指令网络心跳超时");
            command_net_restart();
            passback_net_restart();
        }
        SLEPP_T_S(ticks);  
    }
}
static struct bsockt command_net_handle = \
BCONN_INIT_STRUCTURE("command", 0, 0, command_rec, DEFAULT_TR_OUT);



void command_net_start(unsigned int remote_ip, unsigned short remote_port)
{
    bconn_connect(&command_net_handle, remote_ip, remote_port);
}
void command_net_stop(void)
{
    bconn_stop(&command_net_handle);
}

void command_net_restart(void)
{
    bconn_restart(&command_net_handle);
}

void ebm_adt_command_start(void) {
    bconn_connect(&command_net_handle, ebm_env_get_sip(), syscfg_plat_port_get());
}


void *command_tast(void *parm)
{
    struct bsockt *b = &command_net_handle;
	ebm_adt_command_start();
	while(1)
	{
		bconn_machine(b);
        if(bconn_isConnect(b)) {
            bconn_block_rec(b);
            command_send_func(b);
        }
        usleep(10*1000);
	}
	return 0;
}



extern unsigned char led_visit_rtp_sta(void);
static void led_visit_worksta(void)
{
	if(command_net_handle.fd != FD_INVALID ){
		if(1 == led_visit_rtp_sta()){
			led_ip_work_on();
		}
		else{
			led_ip_work_blink();
		}
	}
	else
		led_ip_work_idle();
}


const char* visit_command_net_sta(void)
{
	const char *sta_str[]={
		"未连接",
		"心跳超时",
		"正常",
	};
	unsigned char ret=0 ;
	if(command_net_handle.fd != FD_INVALID ){
		if(ebm_net_keep_wcnt >= EBM_NET_KEEP_WCNT_VAL/2 )
			ret=2;
		else
			ret=1;
	}
	return sta_str[ret];
		
}




